/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Common;

import Constants.*;
import javax.microedition.io.*;
import java.io.*;

/**
 *
 * @author Hmong
 */
public class Whois {
    public static String GetDomainInfo(String domain, String whoisServer)
    {
        return GetDomainInfo(domain, whoisServer, 43);
    }

    public static String GetDomainInfo(String checkDomain, String whoisServer, int port)
    {
        String strRet = null;

        try
        {
            // socket://com.whois-servers.net
            SocketConnection socketConn = (SocketConnection)Connector.open("socket://" + whoisServer + ":" + Integer.toString(port));

            // 发送 域名 到服务器
            OutputStream outputStream = socketConn.openOutputStream();

            checkDomain += "\r\n";
            outputStream.write(checkDomain.getBytes(Constant.Encoding_Default));

            // 从服务器读取回应内容（域名信息或无匹配记录）
            InputStream inputStream = socketConn.openInputStream();

            byte[] readBuffer = new byte[1024 * 10];
            int nActualRead = inputStream.read(readBuffer);
            if (0 < nActualRead)
                strRet = new String(readBuffer, 0, nActualRead, Constant.Encoding_Default);
            else
                strRet = "[None]";
        }
        catch(Exception ex)
        {
            strRet = "[Error] " + ex.getMessage();
        }

        return strRet;
    }
}
